package com.liao.domain.strategy.service.rule.filter.impl;

import com.liao.domain.strategy.model.entity.RuleActionEntity;
import com.liao.domain.strategy.model.entity.RuleMatterEntity;
import com.liao.domain.strategy.model.valobj.RuleLogicCheckTypeVO;
import com.liao.domain.strategy.repository.IStrategyRepository;
import com.liao.domain.strategy.service.annotation.LogicStrategy;
import com.liao.domain.strategy.service.rule.filter.ILogicFilter;
import com.liao.domain.strategy.service.rule.filter.factory.DefaultLogicFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Author long
 * @Description 用户抽奖n次后，对应奖品可解锁抽奖
 * @Date 2024-08-04 21:43
 */
@Slf4j
@Component
@LogicStrategy(logicMode = DefaultLogicFactory.LogicModel.RULE_LOCK)
public class RuleLockLogicFilter implements ILogicFilter<RuleActionEntity.RaffleCenterEntity> {

    @Resource
    private IStrategyRepository strategyRepository;

    // 用户抽奖次数，后续完成这部分流程开发的时候，从数据库/Redis中读取
    private Long userRaffleCount = 0L;

    @Override
    public RuleActionEntity<RuleActionEntity.RaffleCenterEntity> filter(RuleMatterEntity ruleMatterEntity) {

        //查询该奖品需要抽奖几次才能解锁
        String ruleValue = strategyRepository.queryStrategyRuleValue(ruleMatterEntity.getStrategyId(), ruleMatterEntity.getRuleModel(), ruleMatterEntity.getAwardId());
        Long raffleCount = Long.parseLong(ruleValue);

        if (userRaffleCount >= raffleCount) {
            //达到了解锁的条件，直接返回奖品
            return RuleActionEntity.<RuleActionEntity.RaffleCenterEntity>builder()
                    .code(RuleLogicCheckTypeVO.ALLOW.getCode())
                    .info(RuleLogicCheckTypeVO.ALLOW.getInfo())
                    .build();
        }

        //没有达到解释的条件，规则拦截，给其它奖品
        return RuleActionEntity.<RuleActionEntity.RaffleCenterEntity>builder()
                .code(RuleLogicCheckTypeVO.TAKE_OVER.getCode())
                .info(RuleLogicCheckTypeVO.TAKE_OVER.getInfo())
                .build();
    }
}
